package com.insurance.controller;

import com.insurance.domain.ResponseResult;
import com.insurance.domain.entity.Customer;
import com.insurance.domain.entity.User;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.domain.vo.CustomerPageVo;
import com.insurance.domain.vo.UserPageVo;
import com.insurance.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author qcjstudy
 * @create 2023-03-28 15:31
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    private UserPageVo userPageVo;

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/listAll")
    public ResponseResult listAll(){
        return ResponseResult.okResult(userService.list());
    }

    /**
     * 分页查询所有用户
     * @param
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody UserPageVo userPageVo){
        this.userPageVo = userPageVo;
        return userService.listByPage(userPageVo);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping()
    public ResponseResult add(@RequestBody User user){
        return userService.addUser(user);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody User user){
        return userService.updateUser(user);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id){
        if(userService.removeById(id)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAILED);
    }
}
